#pragma once

#include <mutex>
#include "util.hpp"
#include "log.hpp"

#define defaultfile "./cloud.conf"
namespace cloud
{
    class Config
    {
    private:
        static std::mutex _mutex;

        static Config *_instance; // 单例模式

        Config()
        {
            lg(INFO, "Config()");
            ReadConfidFile();
        }

    private:
        // config damo
        //  "hot_time" : 30,
        // "server_port" : 8080,
        // "server_ip" : "58.87.92.219",
        // "download_prefix" : "/download/",
        // "packfile_suffix" : ".lz",
        // "pack_dir" : "./packdir/",
        // "back_dir" : "/backdir/",
        // "backup_file" : "./could.dat"

        int _host_time;               // 热点文件时间
        int _server_port;             // 服务器端口号
        std::string _server_ip;       // 服务器IP地址
        std::string _download_prefix; // 下载前缀
        std::string _packfile_suffix; // 打包后缀
        std::string _pack_dir;        // 默认备份路径
        std::string _back_dir;        // 默认打包路径
        std::string _back_file;       //

        bool ReadConfidFile()
        {
            std::string body;
            cloud::FileUtil fu(defaultfile);
            bool n = fu.SetContent(body);
            if (!n)
            {
                lg(ERROR, "ReadConfidFile SetContent Error");
                return false;
            }
            Json::Value root;
            bool s = cloud::JsonUtil::UnSerialize(body, &root);
            if (!s)
            {
                lg(ERROR, "ReadConfidFile UnSerialize Error");
                return false;
            }

            _host_time = root["host_time"].asInt();
            _server_port = root["server_port"].asInt();
            _server_ip = root["server_ip"].asString();
            _download_prefix = root["download_prefix"].asString();
            _packfile_suffix = root["packfile_suffix"].asString();
            _pack_dir = root["pack_dir"].asString();
            _back_dir = root["back_dir"].asString();
            _back_file = root["back_file"].asString();

            return true;
        }

    public:
        static Config *GetInstance()
        {
            if (_instance == NULL)
            {
                lg(INFO, "GetInstance  lock");
                _mutex.lock();
                if (_instance == NULL)
                {
                    _instance = new Config();
                }
                _mutex.unlock();
            }
            return _instance;
        }
        int GetHostTime()
        {
            return _host_time;
        }
        int GetServerPort()
        {
            return _server_port;
        }
        std::string GetServerIP()
        {
            return _server_ip;
        }
        std::string GetDownLoadPrefix()
        {
            return _download_prefix;
        }
        std::string GetPackFileSuffdix()
        {
            return _packfile_suffix;
        }
        std::string GetPackDir()
        {
            return _packfile_suffix;
        }
        std::string GetBackDir()
        {
            return _back_dir;
        }
        std::string GetBackFile()
        {
            return _back_file;
        }
    };

    Config *Config::_instance = nullptr;
    std::mutex Config::_mutex;

};